<?php
/**
 * Created by PhpStorm.
 * User: zhang
 * Date: 2018/2/7
 * Time: 14:38
 */

$socket = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
echo 'server start';
if ( socket_bind( $socket, "127.0.0.1", 8888 ) == false ) {
	echo 'server bind fail:' . socket_strerror( socket_last_error() );
}
if ( socket_listen( $socket, 0 ) == false ) {
	echo 'server listen fail:' . socket_strerror( socket_last_error() );
}
while ( true ) {
	$accept = socket_accept( $socket );
	if ( $accept != false ) {
		$string = socket_read( $accept, 1024 );
		echo 'server receive is :' . $string . PHP_EOL;//PHP_EOL为php的换行预定义常量
		if ( $string != false ) {
			$return_string = 'server receive is :' . $string . PHP_EOL;
			$ret           = socket_write( $accept,
				$return_string,
				strlen( $return_string ) );
//			var_dump( $ret );
		} else {
			echo 'socket_read is fail';
		}
		socket_close( $accept );
	}
}
socket_close( $socket );